<!doctype html>
<html lang="zh">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <link rel="icon" href="/static/images/favicons/favicon-32x32.png" sizes="32x32" type="image/png">
    <link rel="icon" href="/static/images/favicons/favicon-16x16.png" sizes="16x16" type="image/png">
    <link rel="stylesheet" href="/static/lib/bootstrap-icons/font/bootstrap-icons.css">
    <link rel="stylesheet" href="/static/css/bootstrap.min.css">
    <link rel="stylesheet" href="/static/css/bootstrap-admin.min.css">
    <title>视频播放器</title>
</head>
<body>

<div class="min-vh-100 d-flex justify-content-center align-items-center py-3">
    <div class="container-fluid">
        <div class="row">
            <div class="col-12 col-sm-8 col-lg-6 col-xl-4 mx-auto">
                <div class="card shadow border-0 bg-white">
                    <div class="card-header bg-white border-bottom p-4 text-center">
                        <a href="#" class="fs-4 text-dark text-decoration-none">
                            视频播放器注册
                        </a>
                    </div>
                    <div class="card-body">
                        <form id="form" class="form">
                            {# 设置tocken,这个只用在前后端不分离的情况下使用 #}
                            {% csrf_token %}
                            <div class="mb-3">
                                <label for="username" class="form-label">账号</label>
                                <input type="text" class="form-control" id="username" name="username"
                                       placeholder="用户名">
                            </div>
                            <div class="mb-3">
                                <label for="password" class="form-label">密码</label>
                                <input type="password" class="form-control" id="password" name="password"
                                       placeholder="请输入密码">
                            </div>
                            <div class="mb-3">
                                <label for="password_confirm" class="form-label">确认密码</label>
                                <input type="password" class="form-control" id="password_confirm"
                                       name="password_confirm"
                                       autocomplete="off">
                            </div>
                            {#  手机号 #}
                            <div class="mb-3">
                                <label for="phone" class="form-label">手机</label>
                                <input type="text" class="form-control" id="phone" name="phone" autocomplete="off">
                            </div>
                            {# 油箱 #}
                            <div class="mb-3">
                                <label for="email" class="form-label">邮箱</label>
                                <input type="text" class="form-control" id="email" name="email" autocomplete="off">
                            </div>
                            {#  验证码 #}
                            <div class="mb-3">
                                <label for="captcha" class="form-label">验证码</label>
                                <div class="input-group mb-3">
                                    <input type="text" class="form-control" id="captcha" name="captcha"
                                           placeholder="请输入验证码">
                                    <img src="/users/generate_codee/" alt="验证码" class="bsa-cursor-pointer"/>
                                </div>
                            </div>
                            {#  性别 #}
                            {#                            <div class="mb-3">#}
                            {#                                <label for="sexRadio1" class="form-label">性别</label>#}
                            {#                                <div>#}
                            {#                                    <div class="form-check form-check-inline">#}
                            {#                                        <input class="form-check-input" type="radio" name="sex" id="sexRadio1"#}
                            {#                                               value="男" checked="" autocomplete="off">#}
                            {#                                        <label class="form-check-label" for="sexRadio1">先生</label>#}
                            {#                                    </div>#}
                            {#                                    <div class="form-check form-check-inline">#}
                            {#                                        <input class="form-check-input" type="radio" name="sex" id="sexRadio2"#}
                            {#                                               value="女" autocomplete="off">#}
                            {#                                        <label class="form-check-label" for="sexRadio2">女士</label>#}
                            {#                                    </div>#}
                            {#                                </div>#}
                            {#                            </div> #}
                            {# 同意条款 #}
                            <div class="col-sm-9 offset-sm-3">
                                <input type="checkbox" class="form-check-input" id="agree2" name="agree2"
                                       data-fv-field="agree2" autocomplete="off">
                                <label class="form-check-label" for="agree2">同意条款协议</label>
                                <div style="display: none;" class="invalid-feedback" data-fv-validator="notEmpty"
                                     data-fv-for="agree2" data-fv-result="NOT_VALIDATED">您必须同意我们的条款和条件
                                </div>
                            </div>
                            {# 立即注册 #}
                            <div class="d-flex align-items-center justify-content-between flex-wrap gap-3"
                                 style="text-align: center;">
                                <button id="login-btn" type="submit" class="btn btn-primary">立即注册</button>
                            </div>
                        </form>
                    </div>

                    <div class="card-footer bg-white">
                        <div class="text-center p-2 text-muted">
                            Copyright © 2024 羽林.
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>


<!--表单验证相关依赖-->
<script src="/static/lib/jquery/dist/jquery.min.js"></script>
<script src="/static/lib/formvalidation/js/formValidation.js"></script>
<script src="/static/lib/formvalidation/js/framework/bootstrap.js"></script>
<script src="/static/lib/formvalidation/js/language/zh_CN.js"></script>
<script src="/static/js/bootstrap.bundle.min.js"></script>
<script src="/static/js/bootstrap-admin.min.js"></script>
<script>
    //前端表单验证
    $('#form').formValidation({
        fields: {
            username: {
                validators: {
                    notEmpty: true,
                    {#提交ajx请求#}
                    remote: {
                        type: 'post',
                        url: '/users/check_username/',
                        headers: {'X-CSRFToken': $('[name="csrfmiddlewaretoken"]').val()},  // 注意放到引号里面
                        data: {
                            type: 'save',
                        },
                        //请求失败的消息
                        message: "系统繁忙,请稍后再试",
                        delay: 1000
                    }
                }
            },
            password: {
                {#    密码的校验#}
                validators: {
                    notEmpty: {
                        message: "密码不能为空"
                    },
                    different: {
                        field: "username",
                        message: "密码不能和用户名相同"
                    }
                }
            },
            password_confirm: {
                validators: {
                    identical: {
                        field: 'password',
                        message: '和密码不一致'
                    }
                }
            },
            sex: {
                validators: {
                    notEmpty: true,
                }
            },
            phone: {
                validators: {
                    notEmpty: true,
                    callback: {
                        callback: function (value, validator, $field) {
                            var reg1 = /^\+86-1[3-9]\d{9}$/;
                            var reg2 = /^1[3-9]\d{9}$/;
                            return reg1.test(value) || reg2.test(value);
                        },
                        message: '手机号错误'
                    }
                },
            },
            email: {
                validators: {
                    notEmpty: true,
                    emailAddress: {
                        message: "email格式错误"
                    }
                }
            },
            agree2: {
                validators: {
                    notEmpty: {
                        message: "您必须同意我们的条款和条件"
                    }
                }
            },
            captcha: {
                validators: {
                    notEmpty: true,
                    remote: {
                        type: 'post',
                        url: '/users/verify_code/',
                        headers: {'X-CSRFToken': $('[name="csrfmiddlewaretoken"]').val()},  // 注意放到引号里面
                        data: {
                            type: 'save',
                        },
                        //请求失败的消息
                        message: "系统繁忙,请稍后再试",
                        delay: 1000
                    }
                }
            },
        }
    }).on('success.form.fv', function (e) {
        //阻止表单提交
        e.preventDefault();
        //得到表单对象
        var $form = $(e.target);
        let data = $form.serialize();

        //得到序列化数据
        $.ajax({
            url: "/users/submit_register/",
            headers: {'X-CSRFToken': $('[name="csrfmiddlewaretoken"]').val()},  // 注意放到引号里面
            method: 'post',
            data: data,
        }).then(function (res) {
            if (res.code === 200) {
                //注册成功
                alert('OK');
                document.location.href='/users/login/'
            } else {
                //注册失败
                alert("ERROR!")
            }
        });
    });


</script>

</body>
</html>
